﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_MyUserControls_TH_QuangCaoAdmin : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Load the grid only the first time the page is loaded
        if (!Page.IsPostBack)
        {
            // Load the categories grid
            BindGrid();
        }
    }

    // Populate the GridView with data
    private void BindGrid()
    {
        QuangCaoBUS objQC = new QuangCaoBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            objDataSet = objQC.SelectQuangCaoAll();
            grid.DataSource = objDataSet.Tables["SelectQuangCaoAll"];
            grid.DataBind();
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
    }

    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Get the ID of the record to be deleted
        string id = grid.DataKeys[e.RowIndex].Value.ToString();
        QuangCaoBUS objQC = new QuangCaoBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            // Execute the delete command
            bool success = objQC.DeleteQuangCao(int.Parse(id));
            // Cancel edit mode
            grid.EditIndex = -1;
            // Display status message
            lblStatus.Text = success ? "Xóa thành công" : "Xóa thất bại";
            // Reload the grid
            BindGrid();
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkbtnDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Bạn có chắc chắn muốn xóa không?' )");
        }
    }
}
